package com.fufan.mtsaas.modules.newtech.xjslimittype.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.fufan.mtsaas.modules.common.bo.request.ResultPack;
import com.fufan.mtsaas.modules.common.bo.response.RequestPojo;
import com.fufan.mtsaas.modules.common.bo.response.ResStatus;
import com.fufan.mtsaas.modules.common.bo.response.Result;
import com.fufan.mtsaas.modules.newtech.xjslimittype.bo.request.XjsLimitQueryRequest;
import com.fufan.mtsaas.modules.newtech.xjslimittype.bo.request.XjsLimitSaveRequest;
import com.fufan.mtsaas.modules.newtech.xjslimittype.bo.response.XjsLimitTypeResponse;
import com.fufan.mtsaas.modules.newtech.xjslimittype.model.XjsLimitType;
import com.fufan.mtsaas.modules.newtech.xjslimittype.dao.XjsLimitTypeDao;
import com.fufan.mtsaas.modules.newtech.xjslimittype.service.XjsLimitTypeService;
import com.fufan.mtsaas.modules.newtech.xjsunlimittype.bo.request.XjsUnLimitQueryRequest;
import com.fufan.mtsaas.modules.newtech.xjsunlimittype.bo.response.XjsUnLimitResponse;
import com.fufan.mtsaas.tools.ExcelCreateUtil;
import com.fufan.mtsaas.tools.ExcelData;
import com.fufan.mtsaas.util.ParamUtil;
import com.github.pagehelper.ISelect;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 限制基础数据表(XjsLimitType)表服务实现类
 *
 * @author Liby
 * @since 2025-01-05 19:40:53
 */
@Service("xjsLimitTypeServiceImpl")
@Primary
@Slf4j
public class XjsLimitTypeServiceImpl extends ServiceImpl<XjsLimitTypeDao, XjsLimitType> implements XjsLimitTypeService {
    @Resource
    private XjsLimitTypeDao xjsLimitTypeDao;

    @Override
    public Result doQueryPage(XjsLimitQueryRequest map) {
        Result<XjsLimitTypeResponse> result = new Result<>();
        Page<XjsLimitTypeResponse> page = new Page<>();
        long total = com.github.pagehelper.PageHelper.count(new ISelect() {
            @Override
            public void doSelect() {
                xjsLimitTypeDao.selectByPage(page, map);
            }
        });


        // 分页
        ParamUtil.setPage(map.getPageSize(), map.getCurrentPage(), page);
        //分页查询
        List<XjsLimitTypeResponse> list = xjsLimitTypeDao.selectByPage(page,map);

        page.setRecords(list);
        page.setTotal((int) total);
        result.setResStatus(ResStatus.STATUS_SUCCESS);
        result.setResMsg(ResStatus.STATUS_SUCCESS_MSG);
        result.setResData(page);
        return result;
    }

    @Override
    public Result doExport(XjsLimitQueryRequest queryRequest, RequestPojo requestPojo, HttpServletResponse response) {

        ResultPack resultPack = new ResultPack();
        Result result = new Result();
        ExcelData excelData = new ExcelData();
        excelData.setName("限制类列表");
        String[] title = {"序号", "医疗技术名称","级别"};
        Page<XjsLimitTypeResponse> page = new Page<>();

        List<XjsLimitTypeResponse> list = xjsLimitTypeDao.selectByPage(page, queryRequest);
        List<List<Object>> rows = new ArrayList<>();

        // HashMap 转换为 List<Object>
        for (XjsLimitTypeResponse item : list) {
            Map<String, Object> mapEntrys = new HashMap<>();
            mapEntrys.put("序号", item.getSysNo());
            mapEntrys.put("医疗技术名称", item.getTechnicalName());
            mapEntrys.put("级别", item.getClassLevel());
            List<Object> row = new ArrayList<>();
            row.addAll(mapEntrys.values());
            rows.add(row);
        }

        List<String> titles = Arrays.asList(title);
        excelData.setTitles(titles);
        excelData.setRows(rows);
        String filename = String.valueOf(System.currentTimeMillis());
        ExcelCreateUtil.exportExcel(response, "limitType" + filename + ".xls", excelData);

        result.setResStatus(ResStatus.STATUS_SUCCESS);
        result.setResMsg(ResStatus.STATUS_SUCCESS_MSG);

        return resultPack.doResultPack(result, "", "111111");
    }


    @Override
    public Result doSave(XjsLimitSaveRequest entity, RequestPojo requestPojo) {
        return null;
    }

    @Override
    public Result doUpdate(XjsLimitSaveRequest entity, RequestPojo requestPojo) {
        return null;
    }

    @Override
    public Result doDelete(XjsLimitQueryRequest map, RequestPojo requestPojo) {
        return null;
    }
}
